public class RestockThread implements Runnable {
    private final CDStore store;
    private final Logger logger;

    public RestockThread(CDStore store, Logger logger) {
        this.store = store;
        this.logger = logger;
    }

    @Override
    public void run() {
        try {
            while (!Thread.currentThread().isInterrupted()) {
                // 优先处理紧急补货
                if (store.needsEmergencyRestock()) {
                    store.restock();
                    store.setNeedsEmergencyRestock(false);
                    logger.log("Emergency restock completed");
                }

                // 执行常规补货
                Thread.sleep(1000);
                if (!store.needsEmergencyRestock()) { // 再次检查避免重复补货
                    store.restock();
                    logger.log("Regular restock completed");
                }
            }
        } catch (InterruptedException e) {
            // 被中断时检查是否需要最后一次紧急补货
            if (store.needsEmergencyRestock()) {
                store.restock();
                store.setNeedsEmergencyRestock(false);
                logger.log("Final emergency restock on shutdown");
            }
            Thread.currentThread().interrupt();
        }
    }
}